package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void add(Role role) {
//补全属性
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
//        添加 数据
        roleMapper.insert(role);
    }


    //    列表查询
    @Override
    public PageResult<Role> list(String name, String label, Integer page, Integer pageSize) {
//设置PageHelper参数
        PageHelper.startPage(page,pageSize);
//        调用Mapper分页查询
        List<Role> rolesList = roleMapper.selectList1(name,label);
//解析并封装结果分页
//        Page<Dept> p= (Page<Dept>)deptList;
            Page<Role> p =(Page<Role>) rolesList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    @Override
    public Role getById(Integer id) {
        return roleMapper.selectById(id);
    }

    @Override
    public void update(Role role) {
//        补全属性
        role.setUpdateTime(LocalDateTime.now());
//        调用Mapper方法
        roleMapper.update(role);
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }
}
